#coding=utf-8
import json
from app.web.comm.check import  Check
# from config.config import  basedir
from app.web.conf.config import *
class OperationJson():
    #读取json文件
    def read_data(self,file_path):
        with open(file_path, encoding='utf-8') as fp:#打开json文件
            data = json.load(fp)#读取json数据
            return data

    #根据关键字获取数据,id代表关键字
    def get_data(self, file_path, id):
        return self.read_data(file_path)[id]

    #写cookie json
    def write_cookie_data(self,data):
        with open(basedir+'\\data\\cookie.json','wb') as fp:#打开cookie文件  'ab'追加的模式写入
            fp.write(data)

    #写token
    def write_token_data(self,data):
        with open (basedir +'\\data\\token.json','wb') as fp:
            fp.write(data)

    #变更json文件的值
    # def check_json_value(self,dict_json,k,v):#dict_json为读取到的json,k设为变量key，v设为变量value
    #     if isinstance(dict_json,dict):#判断是否是字典类型，instance返回true,false
    #         for key in dict_json:#遍历key
    #             if key==k:#如果变量k是json里边的key,则给k赋值v
    #                 dict_json[key]=v
    #             elif isinstance(dict_json[key],dict):#如果dict_json[key]依旧是字典类型
    #                 self.check_json_value(dict_json[key],k,v)#递归
    #     return dict_json

    #变更json文件的值
    def check_json_value(self,data,id,k,v):#data为读取到的json,json_id为json对应的idk设为变量key，v设为变量value
        dict_json=data[id]
        if isinstance(dict_json,dict):#判断是否是字典类型，instance返回true,false
            for key in dict_json:#遍历key
                if key==k:#如果变量k是json里边的key,则给k赋值v
                    dict_json[key]=v
                elif isinstance(dict_json[key],dict):#如果dict_json[key]依旧是字典类型
                    self.check_json_value(dict_json[key],k,v)#递归
        # return dict_json
        return data

#重写json文件json的值
    def rewrite_json(self,file_path,data,id,k,v):
        data_json_new=self.check_json_value(data,id,k,v)
        # print("---------data_json_new=%s"%data_json_new)
        with open(file_path,'w',encoding='utf-8') as f:
            json.dump(data_json_new,f,ensure_ascii=False,indent=4)#indent表示自动换行
        f.close()

    def data_new(self):
        C=Check()
        file = basedir +'data/request_data.json'
        data = self.read_data(file)
        id = 'worker_info'
        key = 'ServiceProviderWorkOrderSave[id]'
        sql_1 = "select id from m_service_provider where username ='%s'"%data['register']['email']
        # sql = "select id from m_service_provider where username ='18121301@gaopeng.com'"
        print(sql_1)
        try:
            value=C.coonect_db(database='fpmerchant', sql=sql_1, charset='utf8')[0][0]#查询的数据格式为元祖
            print(value)
        except:
            value = C.coonect_db(database='fpmerchant', sql=sql_1, charset='utf8')#结果为空的时候
            print(value)
            print(type(value))
        self.rewrite_json(file_path=file,data= data,id=id,k=key,v=value)
        # print(data)
        # print(data['register']['email'])
# 调试1
# if __name__=='__main__':
#     opjson = OperationJson()
#     file = '../data/request_data.json'
#     data_json=opjson.read_data(file)
#     # json_id = 'worker_info'
#     # data_json = opjson.get_data(file_path=file, id=json_id)
#     print("data变更前：")
#     print(data_json)
#     opjson.check_json_value(data_json,'ServiceProviderWorkOrderSave[id]',30050)
#     print("data变更后：")
#     print(data_json)
#     # opjson.rewrite_json(file_path=file,dict_json= data_json,k='ServiceProviderWorkOrderSave[id]',v=350)

    #调试2
# if __name__=='__main__':
#     opjson = OperationJson()
#     file = '../data/request_data.json'
#     data=opjson.read_data(file)
#     # print (data)
#     id = 'worker_info'
#     k='ServiceProviderWorkOrderSave[id]'
#     v=30051
#     # # print(opjson.write_cookie_data(data=b"{'data':'hello'}"))
#     # # opjson.write_token_data(data=b"{'data':'hello'}")
#     #
#     print("data变更前：")
#     print(data)
#     # opjson.check_json_value(data,id,k,v)
#     print("data变更后：")
#     print(data)
#     opjson.rewrite_json(file_path=file,data= data,id=id,k=k,v=v)

#调试
if __name__=="__main__":
    opjson=OperationJson()
    opjson.data_new()

# if __name__=="__main__":
#     C=Check()
#     sql = "select id from m_service_provider where username = '18121307@gaopeng.com'"
#     print(sql)
#     value=C.coonect_db(database='fpmerchant', sql=sql, charset='utf8')[0][0]
#     print (value)
#     print(type(value))


